package com.JDK.ListDemo;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Java ArrayList探究
 * Arrays.asList(T ... a)
 * 所以Arrays.asList只是一个适配器模式的应用，它只是提供了接口，并没有修改底层的那个数组
 */
public class ListDemo {
    /**
     * [apple]
     */
    @Test
    public void test1(){
        List<String> list1 = Arrays.asList("apple");
        System.out.println(list1);
    }

    /**
     * [apple, orange]
     */
    @Test
    public void test2(){
        List<String> list2 = Arrays.asList("apple","orange");
        System.out.println(list2);
    }

    /**
     * apple
     * orange
     * banana
     */
    @Test
    public void test3(){
        List<String> list3 = Arrays.asList("apple","orange","banana");
        for(String str:list3)
            System.out.println(str);
    }

    /**
     * java.lang.UnsupportedOperationException
     * 	at java.util.AbstractList.add(AbstractList.java:148)
     * 	at java.util.AbstractList.add(AbstractList.java:108)
     * 	...
     * 	...
     * 	asList返回的List无法改变
     */
    @Test
    public void test4(){
        String[] array4 = {"apple","orange","banana"};
        List<String> list4 = Arrays.asList(array4);
        list4.remove("apple");
        //list4.add("watermelon");
    }

    /**
     * 用Stream流可以将将数组装箱之后转成List
     */
    @Test
    public void test5(){
        int[] array5 = {1,5,10};
        List<Integer> list5 = Arrays.stream(array5).boxed()
                .collect(Collectors.toList());
        System.out.println(list5);
    }

    /**
     * 添加成功！
     */
    @Test
    public void test6(){
        String[] array6 = {"apple","orange","banana"};
        List<String> list6 = new ArrayList<>(Arrays.asList(array6));
        list6.add("watermelon");
        System.out.println(list6);
    }


}
